<?php
namespace addons\crontab\controller;

use addons\crontab\model\Crontab;
use think\Controller;
use think\Db;
use think\Exception;
use think\Log;
use app\admin\model\information\KisBaseinfo;
use app\common\model\InvShelvesLatest;
use app\common\model\Inventory;
use app\common\model\Temporary as Tempmodel;
use app\admin\controller\inventory\Temporary;
use app\admin\library\Auth as adminAuth;
use app\admin\model\inventory\Wmskc;

/**
 * 定时任务接口,每3分钟，调1次api更新本地档案缓存
 *
 * 以Crontab方式每分钟定时执行,且只可以Cli方式运行
 * @internal
 */
class Kbaseinfo extends Controller
{

    /**
     * 初始化方法,最前且始终执行
     */
    public function _initialize()
    {
        // 只可以以cli方式执行
        if (!$this->request->isCli()) {
            //$this->error('Autotask script only work at client!');
        }

        parent::_initialize();

        // 清除错误
        error_reporting(0);

        // 设置永不超时
        set_time_limit(0);
    }

    /**
     * 执行定时任务
     */
    public function index()
    {
        /*KisBaseinfo::getSupplier(true,false,[],true);
        KisBaseinfo::getDepartment(true);
        KisBaseinfo::getEmployee(true);
        KisBaseinfo::getUser(true);
        KisBaseinfo::getCustomer(true,false,[],true);
        KisBaseinfo::getUnit(true);
        KisBaseinfo::getIcItemClass(true);
        KisBaseinfo::getStock(true);*/

		//E:\wwwroot\wms\public\index.php /addons/crontab/Kbaseinfo/index
        //生成物料和货架的最新关联信息
        $invShData = InvShelvesLatest::addData();
        $invShDatas = db('inv_shelves_latest_view')
        ->field('inv_id id,warehouse_id,warehouse_name,warehouse_number warehouse_code,shelves_id,shelves_name')
        ->select();

        $inventory = new Inventory();
        $inventory->saveAll($invShDatas);

        //更新库区是否空置
        $kc_shelves_ids = db('wms_kc')->where([
            'quantity' => ['GT',0]
        ])
        ->group('shelves_id')
        ->column('shelves_id');

        db('shelves')->execute('update fa_shelves set ifempty=1');
        if($kc_shelves_ids){
            db('shelves')
            ->where([
                'id' => ['IN',$kc_shelves_ids]
            ])
            ->update([
                'ifempty'=>0
            ]);
        }

        //拣货任务，推荐路径
        $material_list = db('material_list')->where([
            'status' => 1,
            'tj_shelves_id' => null
        ])
        ->field('id,inventory_number')
        ->select();
        if($material_list){
            foreach($material_list as $material){
                $lastest_kc = db('wms_kc')->where([
                    'inv_number' => ['EQ',$material['inventory_number']],
                    'quantity' => ['NEQ',0]
                ])
                ->order('updatetime desc')
                ->field('shelves_id,warehouse_id')
                ->find();
                $inv_id = db('inventory')->where('number',$material['inventory_number'])->value('id');
                if($lastest_kc){
                    db('material_list')->where([
                        'id' => $material['id']
                    ])
                    ->update([
                        'warehouse_id' => $lastest_kc['warehouse_id'],
                        'tj_shelves_id' => $lastest_kc['shelves_id'],
                        'inv_id' => $inv_id
                    ]);
                }
            }
        }
        echo 'ok';
    }

    // 自动执行 复核并同步
    public function Autosetting(){
        //按整单处理
        if(config('site.auto_deal_order')){
            $temp = new Tempmodel();
            $order_codes = $temp->where([
                'status' => 0, //待处理单据
                'order_list' => ["NEQ",9]//非盘点单据
            ])
            ->group('order_code')
            ->column('order_code');
            //后台登录 后台权限需要配置 kbaseinfo/autosetting
            adminAuth::instance()->login('autoFH', 'Sily.asd#',0);

            $temporary = new Temporary();
            if($order_codes){
                $temporary->setting($order_codes);
            }else{
                $check_order_codes = $temp->where([
                    'status' => 0, //待处理单据
                    'order_list' => ["EQ",9]//盘点单据
                ])
                ->group('order_code')
                ->column('order_code');
                if($check_order_codes){
                    $temporary->setting($check_order_codes);
                }
            }
            if(!$order_codes&&!$check_order_codes){
                echo '暂无单据';
            }
        }else{
            die('手动处理单据');
        }
    }

    //自动获取味业产品入库单，到wms做库存
    public function synKisProInDataToWMS(){
        Db::startTrans();
        try{
            Wmskc::synKisProInDataToWMS();
            Db::commit();
            echo 'ok';
        }catch(Exception $e){
            Db::rollback();
            dump($e->getMessage());
            Log::error($e->getMessage());
        }
    }
}